package com.ruoyi.voucher.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.voucher.domain.VoucherTemplate;
import com.ruoyi.voucher.domain.dto.req.TemplateAddReqDTO;
import com.ruoyi.voucher.domain.dto.req.VoucherTemplateReqDTO;
import com.ruoyi.voucher.domain.dto.resp.VoucherTemplateRespDTO;
import com.ruoyi.voucher.service.IVoucherTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 凭证模板Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/voucherTemplate")
public class VoucherTemplateController extends BaseController
{
    @Autowired
    private IVoucherTemplateService voucherTemplateService;

    /**
    * @Description: 模版列表
    * @Param: [voucherTemplateReqDto]
    * @return: com.ruoyi.common.core.web.page.TableDataInfo
    * @Author: Yuan
    * @Date: 2025/2/27
    */
    @GetMapping("/templateList")
    public TableDataInfo templateList(VoucherTemplateReqDTO voucherTemplateReqDto){
        startPage();
        List<VoucherTemplateRespDTO> list = voucherTemplateService.selectTemplateList(voucherTemplateReqDto);
        return getDataTable(list);
    }

    @PostMapping("/addTemplate")
    public AjaxResult addTemplate(@RequestBody @Valid TemplateAddReqDTO templateAddReqDTO) throws Exception {
        return toAjax(voucherTemplateService.addTemplate(templateAddReqDTO));
    }

    /**
     * 查询凭证模板列表
     */
    @RequiresPermissions("voucher:voucherTemplate:list")
    @GetMapping("/list")
    public TableDataInfo list(VoucherTemplate voucherTemplate)
    {
        startPage();
        List<VoucherTemplate> list = voucherTemplateService.selectVoucherTemplateList(voucherTemplate);
        return getDataTable(list);
    }

    /**
     * 导出凭证模板列表
     */
    @RequiresPermissions("voucher:voucherTemplate:export")
    @Log(title = "凭证模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoucherTemplate voucherTemplate)
    {
        List<VoucherTemplate> list = voucherTemplateService.selectVoucherTemplateList(voucherTemplate);
        ExcelUtil<VoucherTemplate> util = new ExcelUtil<VoucherTemplate>(VoucherTemplate.class);
        util.exportExcel(response, list, "凭证模板数据");
    }

    /**
     * 获取凭证模板详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) throws Exception {
        return success(voucherTemplateService.selectVoucherTemplateById(id));
    }

    /**
     * 新增凭证模板
     */
    @RequiresPermissions("voucher:voucherTemplate:add")
    @Log(title = "凭证模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid VoucherTemplate voucherTemplate)
    {
        return toAjax(voucherTemplateService.insertVoucherTemplate(voucherTemplate));
    }

    /**
     * 修改凭证模板
     */
    @RequiresPermissions("voucher:voucherTemplate:edit")
    @Log(title = "凭证模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid TemplateAddReqDTO voucherTemplate) throws Exception {
        return toAjax(voucherTemplateService.updateVoucherTemplate(voucherTemplate));
    }

    /**
     * 删除凭证模板
     */
    @RequiresPermissions("voucher:voucherTemplate:remove")
    @Log(title = "凭证模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(voucherTemplateService.deleteVoucherTemplateByIds(ids));
    }
}
